package fun.qu_an.minecraft.mini_games_helper.client.mixin;

import fun.qu_an.minecraft.mini_games_helper.config.MiniGamesHelperConfig;
import net.minecraft.client.Keyboard;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerInteractionManager;
import net.minecraft.client.util.InputUtil;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(Keyboard.class)
public class KeyBoardMixin {
	@Shadow @Final private MinecraftClient client;

	@Inject(method = "processF3(I)Z",
		at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/entity/EntityRenderDispatcher;setRenderHitboxes(Z)V"),
		cancellable = true)
	private void onProcessF3(int key, CallbackInfoReturnable<Boolean> cir) {
		ClientPlayerInteractionManager interactionManager = client.interactionManager;
		if (interactionManager == null
			|| key != InputUtil.GLFW_KEY_B
			|| !MiniGamesHelperConfig.get().isDisableHitBox(interactionManager.getCurrentGameMode())) {
			return;
		}
		client.getEntityRenderDispatcher().setRenderHitboxes(false);
		client.inGameHud.getChatHud().addMessage(Text.of("\"F3 + B\" has been disabled by current server."));
		cir.setReturnValue(true);
	}
}
